package com.youi.service.impl;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.UserorderDao;
import com.youi.entity.Leaderfan;
import com.youi.entity.Leaderprofit;
import com.youi.entity.Leaderprofitdetail;
import com.youi.entity.Leaderstore;
import com.youi.entity.Merchant;
import com.youi.entity.Orderaftersale;
import com.youi.entity.Ordergoods;
import com.youi.entity.Orderprofitdetail;
import com.youi.entity.User;
import com.youi.entity.Useraddress;
import com.youi.entity.Userorder;
import com.youi.entity.Userwechat;
import com.youi.entity.Yibaopayment;
import com.youi.entity.Yibaorefund;
import com.youi.service.LeaderfanService;
import com.youi.service.LeaderprofitService;
import com.youi.service.LeaderprofitdetailService;
import com.youi.service.LeaderstoreService;
import com.youi.service.MerchantService;
import com.youi.service.OrderaftersaleService;
import com.youi.service.OrdergoodsService;
import com.youi.service.OrderprofitdetailService;
import com.youi.service.UserorderService;
import com.youi.service.UserwechatService;
import com.youi.service.YibaopaymentService;
import com.youi.service.YibaorefundService;
import com.youi.third.yibaopay.YibaopayUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;

@Service
public class UserorderServiceImpl extends BaseServiceImpl<Userorder> implements UserorderService{
		
		private UserorderDao userorderDao;
		
		
		@Autowired
		private OrdergoodsService ordergoodsService;
		@Autowired
		private LeaderfanService leaderfanService;
		@Autowired
		private UserwechatService userwechatService;
		@Autowired
		private OrderaftersaleService orderaftersaleService;
		@Autowired
		private YibaopaymentService yibaopaymentService;
		@Autowired
		private YibaorefundService yibaorefundService;
		@Autowired
		private OrderprofitdetailService orderprofitdetailService;
		@Autowired
		private LeaderstoreService leaderstoreService;
		@Autowired
		private LeaderprofitService leaderprofitService;
		@Autowired
		private LeaderprofitdetailService leaderprofitdetailService;
		@Autowired
		private MerchantService merchantService;
		
		@Autowired
		public void setfaultRecordDao(UserorderDao userorderDao){
			this.userorderDao=userorderDao;
			super.setBaseDAO(userorderDao);
		}

		@Override
		public void getUserorderMsg(List<Userorder> userorderList) throws IOException {
			// TODO Auto-generated method stub
			if (userorderList!=null&&userorderList.size()>0) {
				for (Userorder userorder : userorderList) {
					Map<String, Object> prams=new HashMap<String, Object>();
					prams.put(Common.DELSTATUS, DelStatus.NODEL);
					prams.put("orderId", userorder.getId());
					List<Ordergoods> ordergoodList=ordergoodsService.getByParams(prams);
					userorder.setOrdergoodsList(ordergoodList);	
//					prams=new HashMap<String, Object>();
//					prams.put(Common.DELSTATUS, DelStatus.NODEL);
//					Map<String, Object> searchParams=new HashMap<String, Object>();
//					searchParams.put("orderNos", userorder.getOrderNo());
//					Yibaopayment yibaopayment=yibaopaymentService.getByParam(prams, searchParams, null);
//					if (yibaopayment!=null) {
//						prams=new HashMap<String, Object>();
//						prams.put(Common.DELSTATUS, DelStatus.NODEL);
//						prams.put("uniqueOrderNo", yibaopayment.getPaymentNo());
//						Yibaorefund yibaorefund=yibaorefundService.getByParam(prams);
//						if (yibaorefund!=null) {
//							JSONObject data=YibaopayUtil.yibaoGetRefund(yibaorefund.getOrderId(), yibaorefund.getUniqueRefundNo(), yibaorefund.getRefundOrderNo());
//							if (data!=null&&data.getString("code").equals("OPR00000")) {
//								if (data.getString("status").equals("SUCCESS")) {
//									if (userorder.getRefund()==1) {
//										userorder.setRefund(2);
//										userorderDao.saveOrUpdate(userorder);
//										yibaorefund.setStatus(data.getString("status"));
//										yibaorefundService.saveOrUpdate(yibaorefund);
//										userorder.setRefund(2);
//										userorderDao.saveOrUpdate(userorder);
//										yibaorefund.setStatus(data.getString("status"));
//										yibaorefundService.saveOrUpdate(yibaorefund);
//									}
//								}
//							}							
//						}
//					}					
				}
			}
		}

		@Override
		public void getUserorderMsg(Userorder userorder) {
			// TODO Auto-generated method stub
			if (userorder!=null) {				
					Map<String, Object> prams=new HashMap<String, Object>();
					prams.put(Common.DELSTATUS, DelStatus.NODEL);
					prams.put("orderId", userorder.getId());
					List<Ordergoods> ordergoodList=ordergoodsService.getByParams(prams);
					userorder.setOrdergoodsList(ordergoodList);
					prams=new HashMap<String, Object>();
					prams.put(Common.DELSTATUS, DelStatus.NODEL);
					prams.put(Common.ID, userorder.getMerchantId());
					Merchant merchant=merchantService.getByParam(prams);
					userorder.setMerchantPhone(merchant.getMerchantPhone());
			}
		}

		@Override
		public Userorder addUserorder(User user,Useraddress useraddress,Merchant merchant,Integer orderType) {
			// TODO Auto-generated method stub
			String orderNo=ObjectUtil.getOrderNo(1, user.getId().toString());
			Userorder userorder=new Userorder(user.getId(), null, user.getUserNo());
			userorder.setUserName(user.getNickName());
			userorder.setOrderNo(orderNo);
			userorder.setGmtOrder(new Date());
			userorder.setConsignee(useraddress.getContacts());
			userorder.setPhone(useraddress.getPhone());
			userorder.setProvince(useraddress.getProvince());
			userorder.setCity(useraddress.getCity());
			userorder.setDistrict(useraddress.getDistricts());
			userorder.setAddress(useraddress.getAddress());
			userorder.setOrderType(orderType);
			userorder.setMerchantId(merchant.getId());
			userorder.setMerchantCode(merchant.getMerchantCode());
			userorder.setMerchantName(merchant.getName());
			userorder.setDeliveryType("快递配送");
			userorder.setShipmentStatus(0);
			return userorder;
		}
		
		private Integer getLeaderprofitBack(Userorder userorder) {
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("orderNo", userorder.getOrderNo());
			Orderprofitdetail orderprofitdetail=orderprofitdetailService.getByParam(prams);
			if (orderprofitdetail!=null) {
				if (userorder.getLeaderId()!=null) {
					prams=new HashMap<String, Object>();
					prams.put(Common.DELSTATUS, DelStatus.NODEL);
					prams.put("leaderId", userorder.getLeaderId());
					Map<String, Object> sortParams=new HashMap<String, Object>();
					sortParams.put(Common.ID, Common.DESC);
					Leaderstore leaderstore=leaderstoreService.getByParam(prams, null, sortParams);	
					prams=new HashMap<String, Object>();
					prams.put(Common.DELSTATUS, DelStatus.NODEL);
					prams.put("leaderId", leaderstore.getLeaderId());
					Leaderprofit leaderprofit=leaderprofitService.getByParam(prams);
					if (leaderstore.getLevel()==1) {						
						leaderprofit.setTotalAmount(leaderprofit.getTotalAmount()-orderprofitdetail.getFirstAmount());
						leaderprofit.setFrozen(leaderprofit.getFrozen()-orderprofitdetail.getFirstAmount());
						leaderprofitService.saveOrUpdate(leaderprofit);
						leaderprofitdetailService.saveOrUpdate(new Leaderprofitdetail(userorder.getLeaderId(), 2, -orderprofitdetail.getFirstAmount(), "退款支出", userorder.getOrderNo()));
					}else if (leaderstore.getLevel()==2) {
						leaderprofit.setTotalAmount(leaderprofit.getTotalAmount()-orderprofitdetail.getSecondAmount());
						leaderprofit.setFrozen(leaderprofit.getFrozen()-orderprofitdetail.getSecondAmount());
						leaderprofitService.saveOrUpdate(leaderprofit);
						leaderprofitdetailService.saveOrUpdate(new Leaderprofitdetail(userorder.getLeaderId(), 2, -orderprofitdetail.getSecondAmount(), "退款支出", userorder.getOrderNo()));
						prams=new HashMap<String, Object>();
						prams.put(Common.DELSTATUS, DelStatus.NODEL);
						prams.put("storeId", leaderstore.getStoreId());
						prams.put("level", 1);
						Leaderstore topleaderstore=leaderstoreService.getByParam(prams);
						if (topleaderstore!=null) {
							prams=new HashMap<String, Object>();
							prams.put(Common.DELSTATUS, DelStatus.NODEL);
							prams.put("leaderId", topleaderstore.getLeaderId());
							Leaderprofit topleaderprofit=leaderprofitService.getByParam(prams);
							topleaderprofit.setTotalAmount(topleaderprofit.getTotalAmount()-orderprofitdetail.getFirstAmount());
							topleaderprofit.setFrozen(topleaderprofit.getFrozen()-orderprofitdetail.getFirstAmount());
							leaderprofitService.saveOrUpdate(topleaderprofit);
							leaderprofitdetailService.saveOrUpdate(new Leaderprofitdetail(topleaderstore.getLeaderId(), 2, -orderprofitdetail.getFirstAmount(), "退款支出", userorder.getOrderNo())); 
						}
					}
					return 1;
				}
			}
			return 0;						
		}
		
		
		@Override
		public List<Userorder> getByNoOrderprofitdetail() {
			return userorderDao.getByNoOrderprofitdetail() ;
		}
		
		
		@Override
		public List<Userorder> getByNoLeaderprofitdetail() {
			return userorderDao.getByNoLeaderprofitdetail() ;
		}
		
		 @Override
		 public List<Userorder> getByNoLeaderID(){
		     return userorderDao.getByNoLeaderID();
		 }
		 @Override
		 public List<Map> getLeaderprofitdetailStatByLeaderId() {
		     return userorderDao.getLeaderprofitdetailStatByLeaderId();
		 }

	@Override
	public Userorder getForUpdate(Long id) {
		return userorderDao.getForUpdate(id);
	}
}